/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#include "Example_Mass_Spring_Self_CCD_Collision.h"
#include <sxOpenGL_DefaultCameraRenderer.h>
#include <sxOpenGL_Canvas.h>
#include <sxOpenGL_PickingController.h>
#include <sxMassSpringSimulationController.h>
#include <sxOpenGL_DefaultMassSpringMeshRenderer.h>
#include <sxSelfCCD_MSM_CollisionDetectionHandler.h>
#include <sxMSM_TS_CollisionModel.h>
#include <sxMSM_TS_InverseVelocityResponseHandler.h>

#include <sxOBJ_VegaMassSpringMeshStorage.h>
#include <sxVega_TetgenMassSpringMeshStorage.h>

#include <sxCollisionModel.h>
#include <sxDiscreteCollisionModel.h>
#include <sxTriangleSoupCollisionModel.h>

#include <sxMassSpringMesh.h>
#include <sxMassSpringSimulation.h>
#include <sxInverseVelocityResponseHandler.h>
#include <sxEulerCollisionEventPoolAllocator.h>

using Sx::Simulation::Simulations::MassSpringSimulation;
using Sx::Simulation::Collision::Models::MSM_TS_CollisionModel;
using Sx::Simulation::Objects::MassSpringMesh;
using Sx::Physics::Collision::Event::EulerCollisionEventPoolAllocator;
using Sx::SelfCCD::Handlers::Detection::SelfCCD_MSM_CollisionDetectionHandler;
using Sx::Simulation::Collision::Handlers::Response::MSM_TS_InverseVelocityResponseHandler;
using Sx::Simulation::Interface::MassSpringSimulationController;
using Sx::OpenGL::Graphics::Objects::Rendering::OpenGL_DefaultCameraRenderer;
using Sx::Vega::Storage::OBJ_VegaMassSpringMeshStorage;
using Sx::Simulation::Objects::Rendering::OpenGL_DefaultMassSpringMeshRenderer;
using Sx::OpenGL::Graphics::Picking::OpenGL_PickingController;
using Sx::OpenGL::Interface::OpenGL_Canvas;

Example_Mass_Spring_Self_CCD_Collision::Example_Mass_Spring_Self_CCD_Collision(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags) {
	ui.setupUi(this);

	this->openglContext = std::make_shared<OpenGL_Context>();
	this->openglViewport = new Sx::QtInterface::QOpenGL_Viewport(this, this->openglContext);
	this->openglViewport->setRealtime(true);
	this->openglViewport->initialize();

	//--------------------------------------------------------------------------
	// Setup Collision.
	//--------------------------------------------------------------------------
	MSM_TS_CollisionModel* tscm = new MSM_TS_CollisionModel();
	EulerCollisionEventPoolAllocator<MassSpringMesh>* eulerAllocator = new EulerCollisionEventPoolAllocator<MassSpringMesh>();

	SelfCCD_MSM_CollisionDetectionHandler* ccd_msm_cdh = new SelfCCD_MSM_CollisionDetectionHandler();
	MSM_TS_InverseVelocityResponseHandler* msm_ts_ivrh = new MSM_TS_InverseVelocityResponseHandler();

	tscm->setCollisionDetectionHanlder(ccd_msm_cdh);
	tscm->setCollisionResponseHandler(msm_ts_ivrh);
	tscm->setAllocator(eulerAllocator);

	//--------------------------------------------------------------------------
	// Create a new mass-spring simulation.
	//--------------------------------------------------------------------------
	this->massSpringSimulation = std::make_shared<MassSpringSimulation>(tscm);
	this->massSpringSimulation->setTimeStepType(Sx::Simulation::FIXED);
	this->massSpringSimulation->setTimeStep(1.0 / 1000.0f);

	//--------------------------------------------------------------------------
	// Setup a new mass-spring controller.
	// Controller ownership is transfered to the OpenGL Viewport.
	//--------------------------------------------------------------------------
	this->mssController = std::make_shared<MassSpringSimulationController>(this->openglViewport, std::make_shared<OpenGL_Canvas>(this->openglContext), std::make_shared<OpenGL_PickingController>(), std::make_shared<OpenGL_DefaultCameraRenderer>(this->openglContext), this->massSpringSimulation);
	this->mssController->initialize();
	
	//--------------------------------------------------------------------------
	// Create the demo objects based on the provided object files.
	//--------------------------------------------------------------------------
	std::shared_ptr<OBJ_VegaMassSpringMeshStorage> storage = std::make_shared<OBJ_VegaMassSpringMeshStorage>();
	this->object1 = MassSpringMesh::Load(storage.get(), std::make_shared<OpenGL_DefaultMassSpringMeshRenderer>(openglContext, 1.0f, 0.0f, 0.5f), "Box.obj");
	//this->object2 = MassSpringMesh::Load(storage.get(), std::make_shared<OpenGL_DefaultMassSpringMeshRenderer>(openglContext, 0.5f, 1.0f, 0.6f), "t2.obj");
	//this->object3 = MassSpringMesh::Load(storage.get(), std::make_shared<OpenGL_DefaultMassSpringMeshRenderer>(openglContext, 0.6f, 0.9f, 1.0f), "t3.obj");
	this->ground = MassSpringMesh::Load(storage.get(), std::make_shared<OpenGL_DefaultMassSpringMeshRenderer>(openglContext, 0.6f, 0.2f, 0.4f), "ground.obj");

	this->object1->setSubsteps(5);
	//this->object2->setSubsteps(5);
	//this->object3->setSubsteps(5);
	this->ground->setStatic(true);

	this->massSpringSimulation->add(this->ground);
	this->massSpringSimulation->add(this->object1);
	//this->massSpringSimulation->add(this->object2);
	//this->massSpringSimulation->add(this->object3);

	this->openglViewport->setController(this->mssController);
}

Example_Mass_Spring_Self_CCD_Collision::~Example_Mass_Spring_Self_CCD_Collision() {}

void Example_Mass_Spring_Self_CCD_Collision::resizeEvent(QResizeEvent* e) {
	this->openglViewport->resize(this->width(), this->height());
}

void Example_Mass_Spring_Self_CCD_Collision::mousePressEvent(QMouseEvent* e) {}
void Example_Mass_Spring_Self_CCD_Collision::mouseReleaseEvent(QMouseEvent* e) {}
